//
//  NewPhoneController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/10/27.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "NewPhoneController.h"
#import "ResultController.h"

@interface NewPhoneController ()
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UIButton *bindBtn;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;


@end

@implementation NewPhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


-(void)setNavi
{
    self.title = @"更换手机号码";
}

- (void)setUI
{
    self.view.backgroundColor = Color(245, 245, 245);
    [self.progressView setProgress:0.75 animated:YES];
    [self.codeText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    [self.phoneText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (self.codeText.text.length == 6 && self.phoneText.text.length == 11) {
        self.bindBtn.userInteractionEnabled = YES;
        [self.bindBtn setBackgroundColor:MainColor];
    }else
    {
        [self.bindBtn setBackgroundColor:MainBtnColor];
        self.bindBtn.userInteractionEnabled = NO;
    }
}

- (IBAction)getCodeAction:(id)sender {
    [self openCountdown];
}

// 开启倒计时效果
-(void)openCountdown{
    if (self.phoneText.text.length == 11) {
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSDictionary * dict = @{@"token":self.userToken,@"new_phone":self.phoneText.text};
        [HTTPTool POSTForm:[NSString stringWithFormat:@"%@deposit-account/send-new-phone-sms",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (isSuccess) {
                self.redisKey = [responseObject objectForKey:@"data"][@"phone_redis_key"];
                [self.getCodeBtn startWithTime:59 title:@"获取验证码" countDownTitle:@"重新发送" mainColor:[UIColor clearColor] countColor:[UIColor clearColor]];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }else
    {
        [MBProgressHUD showMessage:@"手机号码格式不正确"];
    }
}

- (IBAction)bindAction:(id)sender {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"token":self.userToken,@"sms_code":self.codeText.text,@"phone_redis_key":self.redisKey};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@deposit-account/send-new-phone-sms",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            ResultController * vc = [[ResultController alloc]init];
            vc.imageName = @"icon_手机绑定";
            vc.labelText = [NSString stringWithFormat:@"%@手机号已绑定",[responseObject objectForKey:@"data"][@"phone_mob"]];
            [self.navigationController pushViewController:vc animated:YES];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
